/* D:\ICADDEV\PRJ\ICAD\ICADFONTCOMBOBOX.CPP
 * Copyright (C) 1997-1999 DIMINE Co.LTD. All rights reserved.
 * 
 * $Revision: 1.1.1.1 $ $Date: 2010/03/03 02:57:11 $ 
 * 
 * Abstract
 * 
 * IcadFontComboBox.cpp : implementation file
 * 
 */ 

#include "stdafx.h"
//#include "icad.h"/*DNT*/
#include "dmFontComboBox.h"/*DNT*/
#include "FILE_PATHS.H"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CdmFontComboBox construction/destruction

/******************************************************************************
*+ CdmFontComboBox::CdmFontComboBox - constructor
*
*/

CdmFontComboBox::CdmFontComboBox()
: CComboBox()
	{
	m_getWhichFonts = GET_FONT_SHX;
	m_CountFountType=0;
//	m_truetypeBitmap.LoadBitmap(AFX_IDATA_TRUETYPE);
//	InitMaskFromBitmap(&m_truetypeBitmap, &m_maskBitmap);
	}

/******************************************************************************
*+ CdmFontComboBox::~CdmFontComboBox - destructor
*
*/

CdmFontComboBox::~CdmFontComboBox()
	{
	}


void CdmFontComboBox::OnDestroy() 
	{
	int count;
	FontItem *fontItem;

	// delete the fontItem attached to each entry
	count = GetCount();
	ASSERT(CB_ERR != count);
	if (CB_ERR != count)
		{
		for (int index = 0; index < count; index++)
			{
			fontItem = (FontItem *) GetItemDataPtr(index);
			ASSERT(-1 != (int) fontItem);
			if (-1 != (int) fontItem)
				{
				delete fontItem;
				fontItem = NULL;
				}
			}
		}
	
	CComboBox::OnDestroy();
	}


/////////////////////////////////////////////////////////////////////////////
// CdmFontComboBox operations

/******************************************************************************
*+ CdmFontComboBox::FillFontComboBox
*
*/

void CdmFontComboBox::FillFontComboBox(
	const int bitCode)
	{

	m_getWhichFonts = bitCode;

	// clear the list
	ResetContent();

	// add the appropriate fonts
	if (m_getWhichFonts & GET_FONT_TTF)
		AddTrueTypeFonts();

	if (m_getWhichFonts & GET_FONT_SHX)
		AddCADFonts("*.shx"/*DNT*/, SHX_FONTTYPE);

	if (m_getWhichFonts & GET_FONT_SHP)
		AddCADFonts("*.shp"/*DNT*/, SHP_FONTTYPE);
	}

/******************************************************************************
*+ CdmFontComboBox::AddTrueTypeFonts
*
*/

void CdmFontComboBox::AddTrueTypeFonts(
	void)
	{

	// get all of the fonts in all of the character sets
	LOGFONT logFont;
	logFont.lfCharSet = DEFAULT_CHARSET;
	logFont.lfFaceName[0] = '\0'/*DNT*/;
	logFont.lfPitchAndFamily = 0;

	// enumerate the fonts
	CClientDC dc(NULL);
	::EnumFontFamiliesEx(dc.GetSafeHdc(),
						 &logFont,
						 (FONTENUMPROC)CdmFontComboBox::EnumFontFamExProc,
						 (LPARAM) this,
						 0);
	}

/******************************************************************************
*+ CdmFontComboBox::EnumFontFamExProc
*
*/

int CALLBACK CdmFontComboBox::EnumFontFamExProc(
	ENUMLOGFONTEX *enumLogFontEx,
	NEWTEXTMETRICEX *newTextMetricEx,
	int fontType,
	LPARAM lParam)
	{

	if (TRUETYPE_FONTTYPE & fontType)
		{
		((CdmFontComboBox*)lParam)->AddFont(&enumLogFontEx->elfLogFont,
											  fontType,
											  enumLogFontEx->elfLogFont.lfFaceName);
		}

	return 1;
	}

/******************************************************************************
*+ CdmFontComboBox::AddCADFonts
*
*/

class FontCandidate : public AbstractFileProcessor 
	{
public:
	FontCandidate( CdmFontComboBox& combo, int fontType) 
			: m_comboBox(combo), m_fontType( fontType) {}
	void ProcessFile( LPCTSTR, WIN32_FIND_DATA&);

protected:
	CdmFontComboBox&	m_comboBox;
	int					m_fontType;
	};

void
FontCandidate::ProcessFile( LPCTSTR, WIN32_FIND_DATA& data)
	{
	if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))	// skip directories
		m_comboBox.AddFont(NULL, m_fontType, data.cFileName);
	}

void CdmFontComboBox::AddCADFonts(
	const CString& fileSpec,
	const int fontType)
	{
		// will add files found in path
	FontCandidate	processor( *this, fontType);


	//FilePath	fontPath( G_DM_GLOBALSVAR->m_paths->m_fontPath);
	FilePath	fontPath;	
	fontPath.FindFiles( processor, fileSpec);
	}

/******************************************************************************
*+ CdmFontComboBox::AddFont
*
*/

int CdmFontComboBox::AddFont(
	LOGFONT *logFont,			// i - will be NULL for shx and shp fonts
	DWORD fontType,				// i - always used
	char *fontName)				// i - always used
	{
	int index;
	FontItem *fontItem = NULL;

	// make sure the font isn't already in the combobox;
	// we want each font listed only once
	if (CB_ERR != FindStringExact(-1, (LPCTSTR) fontName))
		return CB_ERR;

	// set up the fontItem
	fontItem = new FontItem;
	ASSERT(fontItem);
	if (!fontItem)
		return CB_ERR;

	if (logFont)							// logFont will be NULL for shx and shp fonts
		fontItem->logFont = *logFont;

	fontItem->fontType = fontType;
	ASSERT(strlen(fontName) < LF_FACESIZE);
	strncpy(fontItem->fontName, fontName, LF_FACESIZE - 1);	// this has to be no more than LF_FACESIZE, including the NULL
	fontItem->fontName[LF_FACESIZE - 1] = '\0'/*DNT*/;

	// insert the font name and set the fontItem
	index = AddString((LPCTSTR) fontItem->fontName);

	if (index < 0)	// failed to add
		{
		delete fontItem;
		return CB_ERR;
		}
	else			// successfully added
		{
		if (CB_ERR == SetItemDataPtr(index, (void *) fontItem))
			{
			DeleteString(index);			// remove the font name if we can't store its data
			delete fontItem;
			return CB_ERR;
			}
		}  

	return index;
	}

/******************************************************************************
*+ CdmFontComboBox::SetCurrentFont
*
*  NOTE - In a dialog that handles multiple styles, the current font may be
*         IDC_IEXP_VARIES_S.
*
*/

void CdmFontComboBox::SetCurrentFont(
	const char* fontName)
	{
	CString tmpName = (strrchr(fontName, '\\')) ? strrchr(fontName, '\\') + 1 : fontName;	// make sure we just have the font name - no path
	m_fontName = tmpName;	// to do - do we want the full path here or just the font filename???????

	int index = FindStringExact(-1, tmpName);
	if (CB_ERR == index)
		{
			// to do - may have to find out if the current style's font is in the list; if it isn't, we probably
			//         have to add it so that it can be displayed and selected - preview should say "Font not found"
		if (CB_ERR == SetCurSel(0))		// remove this when the above is done
			EnableWindow(FALSE);
		}
	else
		{
		if (CB_ERR == SetCurSel(index))
			EnableWindow(FALSE);
		}

	OnSelChange();
	}

/******************************************************************************
*+ CdmFontComboBox::GetLogFont
*
*/

LPLOGFONT CdmFontComboBox::GetLogFont(
	int selection)
	{
	return &GetFontItem(selection)->logFont;
	}

/******************************************************************************
*+ CdmFontComboBox::GetFontType
*
*/

DWORD CdmFontComboBox::GetFontType(
	int selection)
	{
	FontItem *fontItem;

	fontItem = GetFontItem(selection);
	if (!fontItem)
		return CB_ERR;
	if (-1 != (int) fontItem)					// -1 == failed to get FontItem
		return fontItem->fontType;
	else
		return CB_ERR;
	}

/******************************************************************************
*+ CdmFontComboBox::GetFontItem
*
*/

FontItem * CdmFontComboBox::GetFontItem(
	const int selection)
	{
	int tmpSelection = selection;

	if (-1 == tmpSelection)
		tmpSelection = GetCurSel();

	if ((-1 == tmpSelection) || (CB_ERR == tmpSelection))
		{
		CString str;
		GetWindowText(str);
		tmpSelection = FindString(-1, str);
		if (CB_ERR == tmpSelection)
			tmpSelection = 0;
		}

	ASSERT(-1 != (int) GetItemDataPtr(tmpSelection));
	return (FontItem *) GetItemDataPtr(tmpSelection);
	}

/******************************************************************************
*+ CdmFontComboBox::HaveTrueTypeFont
*
*/

bool CdmFontComboBox::HaveTrueTypeFont(
	void)
	{

	DWORD fontType = GetFontType(GetCurSel());
	if ((CB_ERR == fontType) ||
		(SHX_FONTTYPE == fontType) ||
		(SHP_FONTTYPE == fontType))
		return false;
	else
		return true;
	}


#ifdef USE_THIS
/******************************************************************************
*+ CdmFontComboBox::DeleteItem
*
*/

void CdmFontComboBox::DeleteItem(
	LPDELETEITEMSTRUCT item)
	{
	FontItem *fontItem;

	if (-1 == item->itemID)
		return;

	ASSERT(ODT_COMBOBOX == item->CtlType);

	fontItem = GetFontItem(item->itemID);

	// free the FontItem created in CdmFontComboBox::AddFont()
	if (-1 != (int) fontItem)
		delete fontItem;
	}
#endif


BEGIN_MESSAGE_MAP(CdmFontComboBox, CComboBox)
	//{{AFX_MSG_MAP(CdmFontComboBox)
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelChange)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CdmFontComboBox message handlers

void CdmFontComboBox::OnSelChange() 
	{
	// update preview and other controls
	GetParent()->SendMessage(WM_FONT_NAME_CHANGED);
	}


